<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class HenController extends Controller{
	public function actionIndex(){
		return $this->renderPartial('index');
	}
	public function actionSearch($name,$email){
		$model = new DynameicModel(compact('name','email'));
		$model->addRule(['name','email'],'string',['max' =>128])
			->addRule('email','email')
			->validate();
			if ($model->hasErrors) {
				echo "密码验证失败";
			}else{
				echo "密码验证成功";
			}
	}
	public function actionLogin(){
				$post = Yii::$app->request->post();
		if(Yii::$app->request->isPost){
			$sql = "SELECT * FROM users WHERE user_name='{$post['user_name']}' AND is_deleted=0";
			$data = Yii::$app->db->createCommand($sql)->queryOne();
			if(empty($data)){
				//json

				echo "对不起，你输入的用户名不存在";exit;
			}else{
				$pwd = md5($post['pwd']);
				$sql = "SELECT * FROM users WHERE user_name='{$post['user_name']}' AND pwd='{$pwd}' AND is_deleted=0";
				$data = Yii::$app->db->createCommand($sql)->queryOne();
				if(empty($data)){
					echo "对不起，你输入的密码不对";exit;
				}else{
					//登录成功
					$session = Yii::$app->session;
					//处理登录数据
					$login_data = [
						'id' => $data['id'],
						'user_name' => $data['user_name'],
						'phone' => $data['phone'],
					];
					$session->set('userinfo', $login_data);
					echo json_encode(['code'=>200,'msg'=>'登录成功']);exit;
					echo "200";exit;
				}
			}
		}
	}
	public function actionDoindex(){
		/*
		1.用户输入的用户名是否存在
		2.用户输入的密码是否正确
		*/
		$post = Yii::$app->request->post();
		if(Yii::$app->request->isPost){
			$pwd = md5($post['pwd']);
			$sql = "SELECT * FROM users WHERE user_name='{$post['user_name']}' AND pwd='{$pwd}' AND is_deleted=0";
			$post = Yii::$app->db->createCommand($sql)->queryOne();
			if(!empty($post)){
				echo "登录成功";exit;
			}else{
				echo "登录失败";exit;
			}
		}
}